
const weekdays = [
  "星期日",
    "星期一",
    "星期二",
    "星期三",
    "星期四",
    "星期五",
    "星期六",

]

const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

const dateFormat = (fmt, date) =>{
  if (!date){
    date = new Date()
  }
  let ret;
  const opt = {
    "y+": date.getFullYear().toString(),        // 年
    "M+": (date.getMonth() + 1).toString(),     // 月
    "d+": date.getDate().toString(),            // 日
    "H+": date.getHours().toString(),           // 时
    "m+": date.getMinutes().toString(),         // 分
    "s+": date.getSeconds().toString()          // 秒
    // 有其他格式化字符需求可以继续添加，必须转化成字符串
  };
  for (let k in opt) {
    ret = new RegExp("(" + k + ")").exec(fmt);
    if (ret) {
      fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
    }
  }
  return fmt;
}

function add(a,b){
return Math.round(a *100 + b*100)/100
}

function sub(a,b){
  return Math.round(a *100 - b*100)/100
}
function timeToDate(time){
  const [hour,min] = time.split(":")
  const date = new Date();
  date.setHours(hour)
  date.setMinutes(min)
  date.setSeconds(0)
  return date
}

const distance = (point1, point2)=> {

	let [x1, y1] = point1;
	let [x2, y2] = point2;
    let Lat1 = rad(x1); // 纬度
    let Lat2 = rad(x2);
    let a = Lat1 - Lat2;//	两点纬度之差
    let b = rad(y1) - rad(y2); //	经度之差
    let s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) 
        	+ Math.cos(Lat1) * Math.cos(Lat2) * Math.pow(Math.sin(b / 2), 2)));
        //	计算两点距离的公式
    s = s * 6378137.0;//	弧长等于弧度乘地球半径（半径为米）
    s = Math.round(s * 10000) / 10000;//	精确距离的数值
    return s;
}

const getShowDistance = (distance)=>{
    if(distance < 1000){
      return Math.round(distance) + '米'
    }
    return Math.round(distance/100) /10 + "千米"
}

//	角度转换成弧度
const rad =(d) =>{
    return d * Math.PI / 180.00;
};

const getFuzzyLocation = async ()=>{
  return new Promise(function(resolve, reject){
    wx.getFuzzyLocation({
      type:"wgs84"
    ,success(res){
      resolve(res);
    },
    fail: function fail(error) {
      reject(error);
    },
  })
  })
}
const getDayCN = function(date){
  return weekdays[date.getDay()]
}

const getTimeRangeList = (start,end,step)=>{

  let s = new Date()
  let e = new Date()
  const [sh,sm] = start.split(':')
  const [eh,em] = end.split(':')
  s.setHours(sh)
  s.setMinutes(sm)
  e.setHours(eh)
  e.setMinutes(em)
  const range = []
  for (let i=0;i<1000000;i++){

    if (s.valueOf() > e.valueOf()){
      break
    }
    range.push(dateFormat('HH:mm',s))
    s = new Date( s.valueOf() + step * 60 * 1000)
  }
  return range
}

module.exports = {
  formatTime: formatTime,
  dateFormat,
add,
sub,
timeToDate,
distance,
getShowDistance,
getFuzzyLocation,
  getDayCN,
  getTimeRangeList,
}

